package com.spirit.mypopup.pop;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.spirit.mypopup.R;
import com.spirit.mypopup.popup.QMUIDisplayHelper;

/**
 * author : fang.zhang
 * e-mail : initzf@126.com
 * date   : 2019-10-14
 * desc   :
 */
public class MyPop {

    private LinearLayout llGridCode;
    private TextView tvGridCode;
    private ImageView ivGridCode;
    private Context mContext;
    private PopupWindow mPopupWindow;
    private ListView lvPopWindowList;
    //数据源
    private String[] countryGridCode = {"Test A", "Test B", "Test C", "Test D", "Test E", "Test F", "Test G", "Test H", "Test I"};


    public void showPopWindow(Context mContext, View view) {
        this.mContext = mContext;
        initPopupWindow();

        //popupwindow相对view位置x轴偏移量
        View viewTemp = mPopupWindow.getContentView();

        int dp1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200f, mContext.getResources().getDisplayMetrics());
        int dp2 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250f, mContext.getResources().getDisplayMetrics());

        dp1 = QMUIDisplayHelper.getScreenWidth(mContext);

        int mwidth = View.MeasureSpec.makeMeasureSpec(dp1, View.MeasureSpec.EXACTLY);
        int mheight = View.MeasureSpec.makeMeasureSpec(dp2, View.MeasureSpec.EXACTLY);


        viewTemp.measure(mwidth, mheight);

        int width = viewTemp.getMeasuredWidth();

        int xOffset = (view.getWidth() - width) / 2;

        mPopupWindow.showAsDropDown(view, xOffset, 0);
    }


    private void initPopupWindow() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.grid_code_pop_window_listview, null);
        lvPopWindowList = view.findViewById(R.id.lv_pop_window);
        lvPopWindowList.setDivider(null);                                                       //取消ListView分隔线
        lvPopWindowList.setVerticalScrollBarEnabled(false);                                     //隐藏侧滑栏
        lvPopWindowList.setAdapter(new GridCodePopWindowAdapter(countryGridCode, mContext));    //给弹窗ListView设置adapter

        //设置Item监听
        lvPopWindowList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击弹出弹框
                if (mPopupWindow != null && mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                }
            }
        });

        int dp1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200f, mContext.getResources().getDisplayMetrics());
        int dp2 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200f, mContext.getResources().getDisplayMetrics());
        dp1 = QMUIDisplayHelper.getScreenWidth(mContext);

        mPopupWindow = new PopupWindow(view, dp1, dp2, true);

        mPopupWindow.setOutsideTouchable(true);

        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        mPopupWindow.setFocusable(true);

        final Activity activity = (Activity) mContext;

        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 0.7f;//设置阴影透明度
        activity.getWindow().setAttributes(lp);

        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                lp.alpha = 1f;
                activity.getWindow().setAttributes(lp);
            }
        });

    }

}
